$(function(){
    /*tab form*/
    (function(){
        var $inputText = $('#tab form input,#tab form textarea');
        var $btn = $('#tab form button');
        var $loadingImg = $btn.find('img');
        var $span = $('form span');

        //初始化
        $loadingImg.css({"display":"none"});
        /*var L = ($btn.width() - $loadingImg.width()) / 2;
         $loadingImg.css({"left":L + 'px'});
         $(window).resize(function(){
             var L = ($btn.width() - $loadingImg.width()) / 2;
             $loadingImg.css({"left":L + 'px'});
         });*/
        // $btn.attr("disabled",true);
        $span.css({"visibility":"hidden"});

        $inputText.focus(function(){
            $(this).css({
                "border-color":"#fa5c58",
                "background-color":"#ffffff",
                "box-shadow":"0px 0px 0px #fa5c58"
            });
        });
        $inputText.blur(function(){
            $(this).css({
                "border-color":"#f3f3f3",
                "background-color":"#f3f3f3",
            });
        });
        var flag = false;
        $inputText.eq(1).blur(function(){
            var mobilevalid = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/;
            if(!mobilevalid.test($inputText.eq(1).val())){
                $inputText.eq(1).css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                // $inputText.eq(1).val('');
                flag = false;
            }else{
                flag = true;
            }
        });

        var $name = $('#tab form #name');
        var $contact = $('#tab form #contact');
        var $content = $('#tab form textarea');
        /*var name = '';
        var contact = '';
        var content = '';

        $inputText.eq(0).change(function(){
            name = $name.val();
            console.log(contact)
        });
        $inputText.eq(1).change(function(){
            contact = $contact.val();
            console.log(contact)
        });
        $inputText.eq(2).change(function(){
            content = $content.val();
            console.log(content)
        });
        $inputText.eq(2).blur(function(){
             if(flag && content != '' && name != ''){
                $btn.attr("disabled",false);
                console.log('true')
            }else{
                $btn.attr('disabled',true);
                console.log('false')
            }
        });*/

                        
         var time1 = null;
        $btn.click(function(){
            clearInterval(time1);
            //ajax访问php
            var name = $name.val();
            var contact = $contact.val();
            var content = $content.val();

            if(name == ''){
                $name.css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                $loadingImg.css({"display":"none"});
                return false;
            }else if(!flag){
                $contact.css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                $loadingImg.css({"display":"none"});
                return false;
            }else if(content == ''){
                $content.css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                $loadingImg.css({"display":"none"});
                return false;
            }else{
                $(this).css({
                    "background-color":"#b54542",
                    "color":"#b54542"
                });
                $loadingImg.css({"display":"block"});

                //接收表单的URL地址
                var url = "./123.php";
                //需要POST的值，把每个变量都通过&来联接
                var postStr  = "name="+ name +"&contact="+ contact +"&content="+ content;
                //实例化Ajax
                //var ajax = InitAjax();
                var ajax = false;
                //开始初始化XMLHttpRequest对象
                if(window.XMLHttpRequest) { //Mozilla 浏览器
                    ajax = new XMLHttpRequest();
                    if (ajax.overrideMimeType) {//设置MiME类别
                        ajax.overrideMimeType("text/xml");
                    }
                }
                else if (window.ActiveXObject) { // IE浏览器
                    try {
                        ajax = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            ajax = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                    }
                }
                if (!ajax) { // 异常，创建对象实例失败
                    window.alert("不能创建XMLHttpRequest对象实例.");
                    return false;
                }
                //通过Post方式打开连接
                ajax.open("POST", url, true);
                //定义传输的文件HTTP头信息
                ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                //发送POST数据
                ajax.send(postStr);
                //获取执行状态
                ajax.onreadystatechange = function() {

                    $btn.css({
                        "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                    $loadingImg.css({"display":"none"});

                    //如果执行状态成功，那么就把返回信息写到指定的层里
                    if (ajax.readyState == 4 && ajax.status == 200) {
                        console.log(ajax.responseText);
                        $span.css({"visibility":"visible"});

                        if(ajax.responseText === "1"){
                            $span.text("提示：我们已经收到信息，感谢您的配合！").css({"color":"#00ff00"});
                            time1 = setInterval(function(){
                                $span.css({"visibility":"hidden"});
                            },2000);
                            $name.val('');
                            $contact.val('');
                            $content.val('');
                        }else{
                            $span.text("提示：很抱歉，信息发送失败。请尝试").css({"color":"#ff0000"});
                            $('<a href="javascript:;">重新发送！</a>').css({
                                "color":"#00ff00",
                                "text-decoration":"none",
                                "cursor":"pointer"
                            }).appendTo($span);
                            time1 = setInterval(function(){
                                $span.css({"visibility":"hidden"});
                            },2000);
                        }
                        if($span.css('visibility') == "hidden"){
                            clearInterval(time1);
                        }
                    }
                }
            }
        });


    })();

});